package com.jk.kdd.system.controller;

import com.jk.kdd.base.BaseController;
import com.jk.kdd.system.service.SystemRoleService;
import com.jk.kdd.system.service.SystemUserService;
import com.jk.kdd.system.vo.SystemUserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smart4j.framework.util.CastUtil;
import org.smart4j.framework.util.CollectionUtil;
import org.smart4j.plugin.security.SecurityHelper;
import org.smart4j.plugin.security.exception.AuthcException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by DELL on 2016/10/9.
 */
@Controller
@RequestMapping("/")
public class LoginController extends BaseController{

    private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);

    @Resource(name = "systemUserService")
    private SystemUserService systemUserService;
    @Resource(name = "systemRoleService")
    private SystemRoleService systemRoleService;


    /**
     *  登录画面
     */
    @RequestMapping(value="/",method= RequestMethod.GET)
    public String index(HttpServletRequest request, Model model) {
        return "redirect:/login";
    }


    /**
     *  登录画面
     */
    @RequestMapping(value="/login",method= RequestMethod.GET)
    public String login(HttpServletRequest request, Model model) {
        return "login";
    }

    /**
     *  忘记密码
     */
    @RequestMapping(value="/forgotPwd",method= RequestMethod.GET)
    public String forgotPwd(HttpServletRequest request, Model model) {
        return "forgotPwd";
    }


    /**
     *  提交登录请求
     */
    @RequestMapping(value="/loginSubmit",produces = "application/json",method= RequestMethod.POST)
    @ResponseBody
    public HashMap<String,Object> loginSubmit() {
        // 校验验证码
        String code = getRequestStringValue("code");
        String sessionCode = getSessionStringValue("code");
        if (!(code.equalsIgnoreCase(sessionCode))) {  // 忽略验证码大小写
            HashMap<String,Object> param = new HashMap<String,Object>();
            param.put("code",2);
            param.put("msg","验证码不正确");
            return param;
        }

        // 用户名
        String username = getRequestStringValue("username");
        // 密码
        String password = getRequestStringValue("password");
        try {
            SecurityHelper.login(username, password);
            setSessionBaseData(username);
        } catch (AuthcException e) {
            LOGGER.error("login failure", e);
            HashMap<String,Object> param = new HashMap<String,Object>();
            param.put("code",2);
            param.put("msg","账号或密码错误");
            return param;
        }

        // 是否有进入前台权限
        boolean isCanInfoFrontFlag = getSessionBoolValue ("isCanInfoFrontFlag");
        HashMap<String,Object> param = new HashMap<String,Object>();
        param.put("code",1);
        param.put("msg","登录成功");
        param.put("isCanInfoFrontFlag",isCanInfoFrontFlag);
        return param;
  }

    /**
     * 提交注销请求
     */
    @RequestMapping(value="/logout",method= RequestMethod.GET)
    public void logout() {
        SecurityHelper.logout();
    }


    /**
     * 向session设置基本参数
     */
    private void setSessionBaseData(String username) {
        // 设置当前用户
        SystemUserVO userVO = systemUserService.getSystemUserByUsername(username);
        setSessionValue("user",userVO);

        // 设置操作人ID
        String operateId = CastUtil.castString(userVO.getId());
        setSessionValue("operateId",operateId);

        // 设置默认广告主ID
        List<SystemUserVO> manageAdUserList = systemUserService.getManageAdSystemUser(operateId);//取得自己管理的广告主
        //如果自己管理的广告主不为空
        if (CollectionUtil.isNotEmpty(manageAdUserList)) {
            String companyUserId = CastUtil.castString(manageAdUserList.get(0).getId());
            setSessionValue("companyUserId",companyUserId);
        }

        // 是否有进入前台权限
        boolean isCanInfoFrontFlag = CollectionUtil.isNotEmpty(manageAdUserList);
        setSessionValue("isCanInfoFrontFlag",isCanInfoFrontFlag);
     }
}
